/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石数据
 */
import * as data_entity from "../data/entity";
/*
 * 蛋白石组件
 */
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";
/**
 * * 魔法装备触发器
 */
export class EnchantedEquipmentTrigger extends achieve_plan.Template {
    /**
     * * 物品被动事件触发器
     * 
     * @param {server.Player} player 执行物品被动事件的玩家
     */
    protected Trigger(player: server.Player) {
        /**
         * * 装备容器
         */
        const equippable = player.getComponent('minecraft:equippable');
        /**
         * * 物品槽位
         */
        const itemSlot: server.EquipmentSlot[] = Object.values(server.EquipmentSlot);
        // 遍历物品槽位 并 基于物品类型执行对应事件
        itemSlot.map(slot => equippable?.getEquipment(slot)).forEach(
            (item, index) => {
                switch (item?.typeId) {
                    case 'item_equipment:purple_gold_soul_jade': PurpleGoldSoulJade(player, item, itemSlot[index], equippable); break;

                    case 'item_equipment:scarlet_flame_heart': ScarletFlameHeart(player, item, itemSlot[index], equippable); break;

                    case 'item_equipment:starry_night_bead': StarryNightBead(player, item, itemSlot[index], equippable); break;

                    case 'item_equipment:ice_essence_gem': IceEssenceGem(player, item, itemSlot[index], equippable); break;

                    case 'item_equipment:azure_sea_tide': AzureSeaTide(player, item, itemSlot[index], equippable); break;

                    case 'item_tool:magic_crystal_shield': MagicCrystalShield(player); break;

                    case 'item_armor:seas_in_ring': SeasInRing(player); break;

                    default: break;
                }
            }
        )
    };
    public afterPlanEvent() {
        /**
         * * 获取 全体玩家
         */
        const players = server.world.getAllPlayers();
        // 遍历玩家队列
        players.forEach(player => this.Trigger(player));
    };
    /**
     * * 简短的容器构造器
     * 
     * @param nameTag - 容器名称
     */
    static BriefCreate(nameTag: string) {
        return this.Create(nameTag, 100, {});
    };
};
/**
 * * 诸海之环 技能效果
 * 
 * @param {server.Player} player - 穿着装备的玩家
 */
function SeasInRing(player: server.Player) {
    /**
     * * 实体过滤选项
     */
    const options: server.EntityQueryOptions = {
        excludeFamilies: ['monster'],
        location: player.location,
        maxDistance: 8,
        closest: 8
    };
    /**
     * * 实体排序
     */
    const onSort = (entity0: server.Entity, entity1: server.Entity) => {
        const distance0 = math_tools.Vector.distance(player.location, entity0.location);
        const distance1 = math_tools.Vector.distance(player.location, entity1.location);
        return distance0 - distance1;
    };
    /**
     * * 实体筛选
     */
    const onFilter = (target: server.Entity) => {
        return target.id === player.id || target.getDynamicProperty('entity:contract_user') == player.id;
    };
    /**
     * * 实体队列
     */
    const entitys = native_tools.EntitysSort(player.dimension, options, onSort, onFilter);
    // 造成 范围 瞬间治疗 效果
    entitys.forEach(entity => entity.addEffect('instant_health', 1, { amplifier: 2, showParticles: false }));
    // 判断玩家是否在液体内
    if (!player.isInWater) return;
    // 赋予玩家 额外的 状态效果
    player.addEffect('conduit_power', 300, { showParticles: false });
    /**
     * * 生成 珍珠游鱼 实体
     */
    const fish = native_tools.TrySpawnEntity(player.dimension, 'entity_elemental:fish_of_pearl', player.location);
    if (fish instanceof Error) return;
    // 赋予动态属性
    player.getDynamicPropertyIds().forEach(id => fish.setDynamicProperty(id, player.getDynamicProperty(id)));
    // 校准 珍珠游鱼 的属性
    fish.setDynamicProperty('entity:improve', data_entity.max_experience);
    fish.setDynamicProperty('entity:contract_user', player.id);
    fish.setDynamicProperty('entity:unlock', true);
};
/**
 * * 魔晶盾牌 技能效果
 * 
 * @param {server.Player} player - 穿着装备的玩家
 */
function MagicCrystalShield(player: server.Player) {
    // 为玩家附着状态效果
    player.addEffect('absorption', 300, { amplifier: 8, showParticles: false });
    player.addEffect('resistance', 300, { amplifier: 2, showParticles: false });
};
/**
 * * 星夜凝华 技能效果
 * 
 * @param {server.Player} player - 穿着装备的玩家
 */
function StarryNightBead(player: server.Player, item: server.ItemStack, slot: server.EquipmentSlot, equippable?: server.EntityEquippableComponent) {
    // 消耗 1 点耐久
    equippable?.setEquipment(slot, native_tools.AlterDurability(item, 1));
    // 为玩家附着状态效果
    player.addEffect('night_vision', 300, { showParticles: false });
    player.addEffect('haste', 300, { amplifier: 2, showParticles: false });
};
/**
 * * 紫晶魂玉 技能效果
 * 
 * @param {server.Player} player - 穿着装备的玩家
 */
function PurpleGoldSoulJade(player: server.Player, item: server.ItemStack, slot: server.EquipmentSlot, equippable?: server.EntityEquippableComponent) {
    // 消耗 1 点耐久
    equippable?.setEquipment(slot, native_tools.AlterDurability(item, 1));
    // 为玩家附着状态效果
    player.addEffect('village_hero', 300, { showParticles: false });
    player.addEffect('trial_omen', 300, { showParticles: false });
};
/**
 * * 寒冰灵韵 技能效果
 * 
 * @param {server.Player} player - 穿着装备的玩家
 */
function IceEssenceGem(player: server.Player, item: server.ItemStack, slot: server.EquipmentSlot, equippable?: server.EntityEquippableComponent) {
    // 消耗 1 点耐久
    equippable?.setEquipment(slot, native_tools.AlterDurability(item, 1));
    /**
     * * 实体过滤选项
     */
    const options: server.EntityQueryOptions = {
        families: ['monster'],
        location: player.location,
        maxDistance: 24,
        closest: 8
    };
    /**
     * * 实体筛选
     */
    const entitys = player.dimension.getEntities(options);
    // 为玩家附着状态效果
    player.addEffect('speed', 300, { amplifier: 2, showParticles: false });
    // 为怪物附加状态效果
    entitys.forEach(entity => entity.addEffect('slowness', 300, { amplifier: 4 }));
};
/**
 * * 碧海潮生 技能效果
 * 
 * @param {server.Player} player - 穿着装备的玩家
 */
function AzureSeaTide(player: server.Player, item: server.ItemStack, slot: server.EquipmentSlot, equippable?: server.EntityEquippableComponent) {
    // 消耗 1 点耐久
    equippable?.setEquipment(slot, native_tools.AlterDurability(item, 1));
    // 为玩家附着状态效果
    player.addEffect('conduit_power', 300, { showParticles: false });
    player.addEffect('weaving', 300, { showParticles: false });
};
/**
 * * 赤焰灵心 技能效果
 * 
 * @param {server.Player} player - 穿着装备的玩家
 */
function ScarletFlameHeart(player: server.Player, item: server.ItemStack, slot: server.EquipmentSlot, equippable?: server.EntityEquippableComponent) {
    // 消耗 1 点耐久
    equippable?.setEquipment(slot, native_tools.AlterDurability(item, 1));
    // 为玩家附着状态效果
    player.addEffect('fire_resistance', 300, { showParticles: false });
    player.addEffect('strength', 300, { amplifier: 2, showParticles: false });
};